//
//  DetailViewController.m
//  primoPrj
//
//  Created by Giuseppe on 10/01/14.
//
//

#import "DetailViewController.h"


@interface DetailViewController ()

@end

@implementation DetailViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}


- (id)initWithElement:(Element*)element{
    
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        self.view.backgroundColor = [UIColor whiteColor];
        _element = element;

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(70, 70, 180, 150)];
        
        AppDelegate* AppDel = (AppDelegate*)[[UIApplication sharedApplication]delegate];
        imgView.image = [AppDel loadImage: _element.img];

        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 225, 320, 300)
                                                 style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.scrollEnabled = NO;
        
        [self.view addSubview:imgView];
        [self.view addSubview:_tableView];
        self.hidesBottomBarWhenPushed = YES;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    return self;
}


# pragma mark - -- UITABLEVIEW --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
    /*
     if (indexPath.section == 0)
        return 30;
    
    else if (indexPath.section == 1)
        return 40;
    
    else
        return 40;
     */
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    /*
     if (indexPath.section == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"Nome : %@", _element.name];
    }
    if (indexPath.section == 1) {
        cell.textLabel.text = [NSString stringWithFormat:@"Origine : %@", _element.origin];
    }
    if (indexPath.section == 2) {
        cell.textLabel.text = [NSString stringWithFormat:@"Descrizione : %@", _element.description];
    }
    if (indexPath.section == 3) {
        cell.textLabel.text = [NSString stringWithFormat:@"Reminder : %@", _element.date];
    }*/
    
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = [NSString stringWithFormat:@"Nome : %@", _element.name];
            break;
        case 1:
            cell.textLabel.text = [NSString stringWithFormat:@"Origine : %@", _element.origin];
            break;
        case 2:
            cell.textLabel.text = [NSString stringWithFormat:@"Descrizione : %@", _element.description];
            break;
        case 3:
            cell.textLabel.text = [NSString stringWithFormat:@"Reminder : %@", _element.date];
            break;
            
        default:
            break;
    }
    [cell setSelectionStyle: UITableViewCellSelectionStyleNone];
    [cell.textLabel setFont: [UIFont boldSystemFontOfSize:14] ];
    [cell.textLabel setNumberOfLines:  0];
        
    return cell;
}




/* editing ---------------------------------------------------*/




- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if (editing == YES){
        
        // Change views to edit mode.
        NSLog(@"if");
        
        InsertViewController *insertViewController = [[InsertViewController alloc] initWithElement:_element];
        
        insertViewController.title = [_element name];
        [[self navigationController] presentViewController:insertViewController animated:NO completion:nil];
         

    }
    else {
        // Save the changes if needed and change the views to .
        NSLog(@"else");
    }
    
    
    
}


















- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
	// Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated{
    [_tableView reloadData];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
